﻿using MusicExplorer.Common;
using MusicExplorer.Model;
using MusicExplorer.Repository;
using MusicExplorer.Repository.Interfaces;
using MusicExplorer.ViewModel.Wrappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MusicExplorer.ViewModel
{
    public sealed class RepositoryPageViewModel : PageViewModelBase
    {
        private Model.Repository m_repository;

        public RepositoryPageViewModel(HomePageViewModel parent)
            : base(parent)
        {

        }

        private List<GenreWrapper> m_genres;
        public List<GenreWrapper> Genres { get { return m_genres; } }

        protected async override Task InitializeAsync(NavigationObject obj)
        {
            m_repository = (Model.Repository)obj.Entity;

            var classRepository = RepositoryBase.GetInstance(m_repository.ClassName);

            // Step 1 : Initialiser les genres
            if(classRepository is ITracksByGenre)
            {
                var genreRepository = (ITracksByGenre)classRepository;
                m_genres = new List<GenreWrapper>();

                var genres = await genreRepository.GetGenres();
                //var source = await RepositoryHelper.GetLocalSourceAsync(
                //    string.Format("ms-appx:///Data/{0}/{0}_searchgenres.htm", m_repository.Id));
                //var genres = await genreRepository.DecodeGenres(source);
                foreach (var genre in genres)
                    m_genres.Add(new GenreWrapper(this, genre));
            }

            //var genres = await RepositoryHelper.GetRepositoriesAsync();
            //foreach (var repo in repositories)
            //    m_repositories.Add(new RepositoryWrapper(this, repo));
        }

        public override string Title { get { return m_repository.Name; } }

        public Model.Repository Repository { get { return m_repository; } }
    }
}
